/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.persistence.vfs;

import org.proteusframework.core.api.model.INamespace;
import org.proteusframework.core.base.Namespace;
import org.proteusframework.platformservice.persistence.api.IVFSFile;

import java.io.File;
import java.net.URL;
import java.util.Properties;

/**
 * @author Tacoma Four
 */
public class VFSFile extends Namespace implements IVFSFile
{
    protected final FilePath filePath;
    protected final File currentProjectVFSRoot;
    protected final File mappedFile;

    public VFSFile(INamespace namespace, File currentProjectVFSRoot, FilePath filePath)
    {
        super(namespace);
        this.filePath = filePath;
        this.currentProjectVFSRoot = currentProjectVFSRoot;
        mappedFile = new File(this.currentProjectVFSRoot, filePath.toString());
    }

    public VFSFile(String namespace, String id, File currentProjectVFSRoot, FilePath filePath)
    {
        super(namespace, id);
        this.filePath = filePath;
        this.currentProjectVFSRoot = currentProjectVFSRoot;
        mappedFile = new File(this.currentProjectVFSRoot, filePath.toString());
    }

    public VFSFile(String namespace, String id, String refId, File currentProjectVFSRoot, FilePath filePath)
    {
        super(namespace, id, refId);
        this.filePath = filePath;
        this.currentProjectVFSRoot = currentProjectVFSRoot;
        mappedFile = new File(this.currentProjectVFSRoot, filePath.toString());
    }

    @Override
    public File getFile()
    {
        return mappedFile;
    }

    @Override
    public long length()
    {
        return mappedFile.length();
    }

    @Override
    public boolean canExecute()
    {
        return mappedFile.canExecute();
    }

    @Override
    public boolean canRead()
    {
        return mappedFile.canRead();
    }

    @Override
    public boolean canWrite()
    {
        return mappedFile.canWrite();
    }

    @Override
    public int compareTo(File pathname)
    {
        return mappedFile.compareTo(pathname);
    }

    @Override
    public int compareTo(IVFSFile file)
    {
        return getPath().compareTo(file.getPath());
    }

    @Override
    public boolean exists()
    {
        return mappedFile.exists();
    }

    @Override
    public String getName()
    {
        return mappedFile.getName();
    }

    @Override
    public FilePath getParentFilePath()
    {
        return filePath.getParent();
    }

    @Override
    public String getPath()
    {
        return filePath.toString();
    }

    @Override
    public FilePath getFilePath()
    {
        return filePath;
    }

    @Override
    public String getAbsolutePath()
    {
        return mappedFile.getAbsolutePath();
    }

    @Override
    public boolean isDirectory()
    {
        return mappedFile.isDirectory();
    }

    @Override
    public boolean isFile()
    {
        return mappedFile.isFile();
    }

    @Override
    public boolean isHidden()
    {
        return mappedFile.isHidden();
    }

    @Override
    public long lastModified()
    {
        return mappedFile.lastModified();
    }

    @Override
    public boolean setExecutable(boolean executableFlag)
    {
        return mappedFile.setExecutable(executableFlag);
    }

    @Override
    public boolean setExecutable(boolean executableFlag, boolean ownerOnlyFlag)
    {
        return mappedFile.setExecutable(executableFlag, ownerOnlyFlag);
    }

    @Override
    public boolean setLastModified(long time)
    {
        return mappedFile.setLastModified(time);
    }

    @Override
    public boolean setReadable(boolean readableFlag)
    {
        return mappedFile.setReadable(readableFlag);
    }

    @Override
    public boolean setReadable(boolean readableFlag, boolean ownerOnlyFlag)
    {
        return mappedFile.setReadable(readableFlag, ownerOnlyFlag);
    }

    @Override
    public boolean setReadOnly()
    {
        return mappedFile.setReadOnly();
    }

    @Override
    public boolean setWritable(boolean writableFlag)
    {
        return mappedFile.setWritable(writableFlag);
    }

    @Override
    public boolean setWritable(boolean writableFlag, boolean ownerOnlyFlag)
    {
        return setWritable(writableFlag, ownerOnlyFlag);
    }

    @Override
    public boolean supportsIcon()
    {
        return false;
    }

    @Override
    public URL getGlyphURL()
    {
        // TODO: Are we going to support an icon association with a VFS file?
        return null;
    }

    @Override
    public boolean supportsMetadata()
    {
        return false;
    }

    @Override
    public Properties getMetadata()
    {
        return new Properties();
    }
}
